package com.shuimin.service.util;


import pond.common.S;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by sky on 15/11/18.
 */
public class DateUtil {

    public static final long ONE_DAY=24*3600*1000;

    /**
     * 计算一天的开始和结束时间
     * @param date
     * @return
     */
    public static Long[] getDayStartEnd(long date){
        Calendar c=Calendar.getInstance();
        c.setTimeInMillis(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND, 0);
        long start=c.getTimeInMillis();
       return new Long[]{start,start+ONE_DAY-1};
    }

    /**
     * 返回一段时间中每一天的开始时间
     * @param start_time
     * @param end_time
     * @return
     */
    public static List<Long> getDays(long start_time, long end_time){
        List<Long> days=new ArrayList<>();
        Calendar c=Calendar.getInstance();
        c.setTimeInMillis(start_time);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND, 0);

        while (c.getTimeInMillis()<end_time){
            days.add(c.getTimeInMillis());
            c.add(Calendar.HOUR,24);
        }
        return days;
    }

    public static Long[] getMonthStartEnd(long month){
        Calendar c=Calendar.getInstance();
        c.setTimeInMillis(month);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND, 0);
        long start=c.getTimeInMillis();
        c.add(Calendar.MONTH, 1);
        return new Long[]{start,c.getTimeInMillis()-1};
    }

    /**
     * 获得当前月往前推六个月以内的开始结束时间
     * @param month
     * @return
     */
    public static Long[] getSixMonthStartEnd(long month){
        Calendar c=Calendar.getInstance();
        c.setTimeInMillis(month);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND, 0);
        c.add(Calendar.MONTH, 1);
        long end=c.getTimeInMillis()-1;
        c.add(Calendar.MONTH,-6);
        return new Long[]{c.getTimeInMillis(),end};
    }

    public static boolean isToday(long time){
        Calendar c=Calendar.getInstance();
        c.setTimeInMillis(S.now());
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND, 0);
        if(time<c.getTimeInMillis()) return false;
        c.add(Calendar.HOUR_OF_DAY,24);
        if(time>c.getTimeInMillis()) return false;
        return true;
    }

    public static long merge(Date day,Date time){
        Calendar c=Calendar.getInstance();
        c.setTime(day);
        Calendar _c=Calendar.getInstance();
        _c.setTime(time);
        c.set(Calendar.HOUR_OF_DAY, _c.get(Calendar.HOUR_OF_DAY));
        c.set(Calendar.MINUTE,_c.get(Calendar.MINUTE));
        return c.getTimeInMillis();
    }

    public static Date nextMidNight(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return  cal.getTime();
    }

    public static void main(String[] args) {
//        Long[] time=getDayStartEnd(S.now());
//        System.out.println(new Date(time[0]));
//        System.out.println(new Date(time[1]));
//        System.out.println(new Date(1419696000000l));
        System.out.println(isToday(S.now()));
    }
}
